package com.lonely.fx.tree_view;

import com.lonely.fx.tree_view.entity.TestCheckBoxTreeCellDto;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @date: 2022-08-04 10:19
 * @author: ztkj-hzb
 * @description: treeview 的 cellfactory 使用 自定义的效果演示
 **/
public class TreeViewCellFactoryWithCustomerSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();

        //创建TreeView
        TreeView<TestCheckBoxTreeCellDto> treeView = new TreeView<>();

        //准备数据

        //创建根节点
        TreeItem<TestCheckBoxTreeCellDto> root = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "中国"));

        TreeItem<TestCheckBoxTreeCellDto> hb = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "湖北省"));
        TreeItem<TestCheckBoxTreeCellDto> hb_1 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "鄂州市"));
        TreeItem<TestCheckBoxTreeCellDto> hb_2 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "武汉市"));
        TreeItem<TestCheckBoxTreeCellDto> hb_3 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "襄樊市"));
        TreeItem<TestCheckBoxTreeCellDto> hb_4 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "黄冈市"));

        //添加子节点
        hb.getChildren().addAll(hb_1, hb_2, hb_3, hb_4);

        TreeItem<TestCheckBoxTreeCellDto> gd = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "广东省"));
        TreeItem<TestCheckBoxTreeCellDto> gd_1 = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "广州市"));
        TreeItem<TestCheckBoxTreeCellDto> gd_2 = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "深圳市"));
        TreeItem<TestCheckBoxTreeCellDto> gd_3 = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "东莞市"));
        gd.getChildren().addAll(gd_1, gd_2, gd_3);

        TreeItem<TestCheckBoxTreeCellDto> hn = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "湖南省"));
        TreeItem<TestCheckBoxTreeCellDto> hn_1 = new TreeItem<>(new TestCheckBoxTreeCellDto(false, "长沙市"));
        TreeItem<TestCheckBoxTreeCellDto> hn_2 = new TreeItem<>(new TestCheckBoxTreeCellDto(true, "岳阳市"));
        hn.getChildren().addAll(hn_1, hn_2);

        root.getChildren().addAll(hb, gd, hn);

        //设置根节点
        treeView.setRoot(root);

        //设置宽高
        treeView.setPrefSize(300, 300);

        //设置是否展开子节点列表
        root.setExpanded(true);


        anchorPane.getChildren().addAll(treeView);
        AnchorPane.setTopAnchor(treeView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        //设置编辑单元格样式
        treeView.setEditable(true);
        treeView.setCellFactory(new Callback<TreeView<TestCheckBoxTreeCellDto>, TreeCell<TestCheckBoxTreeCellDto>>() {
            @Override
            public TreeCell<TestCheckBoxTreeCellDto> call(TreeView<TestCheckBoxTreeCellDto> param) {
                return new TreeCell<TestCheckBoxTreeCellDto>() {

                    /**
                     *  自己实现一个 checkboxtreecell，且带有checkbox事件的 组件
                     */
                    @Override
                    protected void updateItem(TestCheckBoxTreeCellDto item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            //TODO 这里一定要清楚内容，否则会出现多余数据
                            this.setGraphic(null);
                            this.setText(null);
                            return;
                        }

                        CheckBox checkBox = new CheckBox();
                        checkBox.selectedProperty().bindBidirectional(item.selectedProperty());

                        //TODO 修改展开和收起的图标
                        TreeItem<TestCheckBoxTreeCellDto> currentTreeItem = this.getTreeItem();
                        if (!currentTreeItem.isLeaf()) {
                            //非叶子节点
                            if (currentTreeItem.isExpanded()) {
                                this.setDisclosureNode(new ImageView(new Image("icons/收起.png", 20, 20, true, true)));
                            } else {
                                this.setDisclosureNode(new ImageView(new Image("icons/展开.png", 20, 20, true, true)));
                            }
                        }

                        this.setGraphic(checkBox);
                        this.setText(item.getData());


                        //可以添加checkbox事件，这里演示如果选中父节点，则选中所有子节点效果
                        checkBox.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                                System.out.println(String.format("%s -- %s ", item.getData(), item.isSelected() ? "选中" : "取消"));

                                if (!currentTreeItem.isLeaf()) {
                                    //非叶子节点
                                    for (TreeItem<TestCheckBoxTreeCellDto> child : currentTreeItem.getChildren()) {
                                        child.getValue().setSelected(item.isSelected());
                                    }
                                }

                            }
                        });

                    }
                };
            }
        });


        root.addEventHandler(TreeItem.treeNotificationEvent(), new EventHandler<TreeItem.TreeModificationEvent<Object>>() {
            @Override
            public void handle(TreeItem.TreeModificationEvent<Object> event) {
                System.out.println(event.getEventType());
            }
        });

    }
}
